﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HHCommon;
using HHModel;
using HHBLL;
using System.IO;
namespace HHWeb.SystemManage.DownCores
{
    public partial class Dispose : Base
    {
        Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        HHBLL.DownCores D = new HHBLL.DownCores();
        HHModel.DownCores M = new HHModel.DownCores();
        HHCommon.PagePower pagepower = new HHCommon.PagePower();
        public string DownCoreClassID = string.Empty;
        public string DownCoreClass = string.Empty;
        public string DownCoreClassName = string.Empty;
        public string DownCodeNumber = string.Empty;
        public string FileName = string.Empty;
        public string FileUrl = string.Empty;
        public string FileContent = string.Empty;
        public int ClickRate =0;
        public int DownRate =0;
        public int IfNeed =1;
        public string PublishTime = string.Empty;
        public string FileSource = string.Empty;
        public int Sort =0;
        public int Status =1;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public string Command = "Add";
        protected void Page_Load(object sender, EventArgs e)
        {
            string Message = string.Empty;
            try
            {
                if (help.GetHttpMethodBool())
                {
                    switch (Request.Form["Command"].ToString())
                    {
                        case "Add":
                            Message = Add();
                            break;
                        case "Update":
                            Message = Update();
                            break;
                        default:
                            break;
                    }
                    if (Message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, Message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page, Message, "javascript:history.go(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int ID = int.Parse(Request.QueryString["ID"].ToString());
                        M = D.GetModel(ID);
                        if (M != null)
                        {
                            M = D.GetModel(ID);
                            DownCoreClassID = M.DownCoreClassID.ToString();
                            DownCoreClassName = M.DownCoreClassName;
                            DownCodeNumber = M.DownCodeNumber;
                            FileName = M.FileName;
                            FileUrl = M.FileUrl;
                            this.FCKeditor1.Value = M.FileContent;
                            ClickRate = Convert.ToInt32(M.ClickRate);
                            DownRate = Convert.ToInt32(M.DownRate);
                            IfNeed = Convert.ToInt32(M.IfNeed);
                            PublishTime = M.PublishTime;
                            FileSource = M.FileSource;
                            Sort = Convert.ToInt32(M.Sort);
                            Status = Convert.ToInt32(M.Status);
                            Creater = M.Creater;
                            CreateTime = M.CreateTime.ToString();
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        DownCodeNumber = "XZ-" + help.GetRandom();
                        PublishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
                //下载中心类别（无限极下拉框）
                GetDownCoreClass();
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //下载中心类别（无限极下拉框）
        public void GetDownCoreClass()
        {
            DownCoreClass = pagepower.GetPagePowerInfo("Select [ID] as ID, [Title] as Name, [ParentID] as ParentID from DownCoreClass with(nolock) where Status=1 order by sort desc");
        }
        #endregion

        #region //添加上传文件
        public string Add()
        {
            M.DownCoreClassID = int.Parse(Request.Form["DownCoreClassID"].ToString());
            string DownCoreClassName = Request.Form["DownCoreClassName"].ToString();
            M.DownCoreClassName = DownCoreClassName.Remove(0,DownCoreClassName.LastIndexOf('-')+1);
            M.DownCodeNumber = Request.Form["DownCodeNumber"].ToString();
            M.FileName = Request.Form["FileName"].ToString();
            #region //文件上传
            HttpPostedFile hpf =File.PostedFile;
            string fileName = Path.GetFileName(hpf.FileName);
            string ex = Path.GetExtension(fileName);//获取文件后缀名
            if (string.IsNullOrEmpty(fileName))
                return "上传文件不能为空！";
            string SavaUrl = string.Empty;
            if (ex == ".doc" || ex == ".docx" || ex == ".xls" || ex == ".pdf" || ex == ".wps")
            {
                SavaUrl = "../../MYFiles/DownFiles/" + fileName;
                if (System.IO.File.Exists(Server.MapPath(SavaUrl)))
                    return "文件已存在！";
                hpf.SaveAs(Server.MapPath(SavaUrl));//保存图片路经
                M.FileUrl = SavaUrl;
            }
            else
                return "文件格式错误！";
            #endregion
            M.FileContent = this.FCKeditor1.Value;
            M.ClickRate =int.Parse(Request.Form["ClickRate"].ToString());
            M.DownRate =int.Parse( Request.Form["DownRate"].ToString());
            M.IfNeed =Convert.ToInt32(Request.Form["IfNeed"]);
            M.PublishTime = Request.Form["PublishTime"].ToString();
            M.FileSource = Request.Form["FileSource"].ToString(); 
            M.Sort =int.Parse(Request.Form["Sort"].ToString());  
            M.Status =int.Parse(Request.Form["Status"].ToString());  
            M.Creater = Request.Form["Creater"].ToString();  
            M.CreateTime =Convert.ToDateTime(Request.Form["CreateTime"].ToString());
            if (D.Add(M) > 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //修改上传文件
        public string Update()
        {
            M.ID=int.Parse(Request.QueryString["ID"].ToString());
            M.DownCoreClassID = int.Parse(Request.Form["DownCoreClassID"].ToString());
            string DownCoreClassName = Request.Form["DownCoreClassName"].ToString();
            M.DownCoreClassName = DownCoreClassName.Remove(0, DownCoreClassName.LastIndexOf('-') + 1);
            M.DownCodeNumber = Request.Form["DownCodeNumber"].ToString();
            M.FileName = Request.Form["FileName"].ToString();
            #region //上传文件
            HttpPostedFile hpf =File.PostedFile;
            string fileName = Path.GetFileName(hpf.FileName);
            string ex = Path.GetExtension(fileName);//获取文件后缀名
            string SavaUrl = string.Empty;
            if (!string.IsNullOrEmpty(fileName))
            {
                if (ex == ".doc" || ex == ".docx" || ex == ".xls" || ex == ".pdf" || ex == ".wps")
                {
                    SavaUrl = "../../MYFiles/DownFiles/" +fileName;
                    if (System.IO.File.Exists(Server.MapPath(SavaUrl)))
                        return "文件已存在！";
                    if (System.IO.File.Exists(Server.MapPath(Request.Form["HDFileUrl"].ToString())))
                        System.IO.File.Delete(Server.MapPath(Request.Form["HDFileUrl"].ToString()));
                    hpf.SaveAs(Server.MapPath(SavaUrl));//保存图片路经
                }
                else
                    return "文件格式错误！";
                M.FileUrl = SavaUrl;
            }
            else
                M.FileUrl = Request.Form["HDFileUrl"].ToString();
            #endregion
            M.FileContent = this.FCKeditor1.Value;
            M.ClickRate = int.Parse(Request.Form["ClickRate"].ToString());
            M.DownRate = int.Parse(Request.Form["DownRate"].ToString());
            M.IfNeed =Convert.ToInt32(Request.Form["IfNeed"]);
            M.PublishTime = Request.Form["PublishTime"].ToString();
            M.FileSource = Request.Form["FileSource"].ToString();
            M.Sort = int.Parse(Request.Form["Sort"].ToString());
            M.Status = int.Parse(Request.Form["Status"].ToString());
            M.Creater = Request.Form["Creater"].ToString();
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"].ToString());
            if (D.Update(M))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}